//
//  HomePageTableViewController.m
//  Mtime2.0
//
//  Created by 凉气的Mac on 16/9/3.
//  Copyright © 2016年 凉气的Mac. All rights reserved.
//

#import "HomePageTableViewController.h"

#import "HomePageHeaderView.h"

#import "HomePageCell1.h"

#import "HomePageCell2.h"

#import "HomePageCell3.h"

#import "HomePageCell4.h"

#import "HomePageCell5.h"

#import "HomePageCell6.h"

#import "HomePageCell7.h"

#import "HomePageModel.h"


static NSInteger pageIndex = 2;

@interface HomePageTableViewController ()

@property(nonatomic,strong)  NSArray *modelArray;

@end

@implementation HomePageTableViewController




-(void)setModelArray:(NSArray *)modelArray{

    _modelArray = modelArray;
    
    [self.tableView reloadData];

}




-(void)loadView{

    UITableView *tableView = [[UITableView alloc]initWithFrame:kScreen style:UITableViewStyleGrouped];
    
    self.tableView = tableView;
    
    tableView.delegate = self;
    
    tableView.dataSource = self;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
 
    
    [HomePageModel homePageModelWithURL:@"PageSubArea/GetHomeFeed.api?pageIndex=1" Success:^(NSArray *modelArray) {
        
        self.modelArray = modelArray;
        
    } error:^{
        
    }];
    
    
    
    HomePageHeaderView *headerView = [[HomePageHeaderView alloc]init];
    
    headerView.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.hidden = YES;

    self.tableView.tableHeaderView = headerView;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[HomePageCell1 class] forCellReuseIdentifier:@"cell1"];
    
    [self.tableView registerClass:[HomePageCell2 class] forCellReuseIdentifier:@"cell2"];
    
    [self.tableView registerClass:[HomePageCell3 class] forCellReuseIdentifier:@"cell3"];
    
    [self.tableView registerClass:[HomePageCell4 class] forCellReuseIdentifier:@"cell4"];
    
    [self.tableView registerClass:[HomePageCell5 class] forCellReuseIdentifier:@"cell5"];
    
    [self.tableView registerClass:[HomePageCell6 class] forCellReuseIdentifier:@"cell6"];
    
    [self.tableView registerClass:[HomePageCell7 class] forCellReuseIdentifier:@"cell7"];
    
   // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 50, 0, 0);
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.estimatedRowHeight = 150;
    
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 500, 0, 500);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section < 3) {
        return 1;
    }else{
    
        return  self.modelArray.count;
    }
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell;
    
     HomePageModel *model = self.modelArray[indexPath.row];
    
    
    if (indexPath.section == 0) {
        
         cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" ];
        
    }else if(indexPath.section == 1){
    
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" ];
    
    }else if(indexPath.section == 2){
    
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" ];
        
    }else{
        
      HomePageCell4 * cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell4" ];
        
        if ([model.tag isEqualToString:@"获奖公布"] || [model.tag isEqualToString:@"猜电影"]) {
            
               cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell5" ];
        }
        
        if ([model.tag isEqualToString:@"日韩新片"] || [model.tag isEqualToString:@"欧美新片"]) {
            
            cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell6" ];
        }
        
        if ([model.tag isEqualToString:@"图集"]) {
            
            cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell7" ];
        }
        
        cell1.model = model;
        
        return cell1;
        
    }
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{


    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 1;

}


#pragma mark --
#pragma mark --tableView代理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //CGPoint offsetPoint = scrollView.contentOffset;
    
//    if (offsetPoint.y < -20) {
//        
//        offsetPoint.y = -20;
//        
//        scrollView.contentOffset = offsetPoint;
//        
//    }
//    if (scrollView.contentSize.height > 2600) {
//        
//        return;
//    }
    
    
    if (scrollView.contentOffset.y + scrollView.height > scrollView.contentSize.height ) {
        
        [self addData];
        
    }
    
   
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSLog(@"%f",scrollView.contentSize.height);
    
     NSLog(@"%f---",scrollView.contentOffset.y);

}


#pragma mark --
#pragma mark --下拉添加数据
-(void)addData{

    NSMutableArray *muArray = [NSMutableArray arrayWithArray:self.modelArray];
    
    NSString *urlString = [NSString stringWithFormat:@"PageSubArea/GetHomeFeed.api?pageIndex=%zd",pageIndex];
    
    
    [HomePageModel homePageModelWithURL:urlString Success:^(NSArray *modelArray) {
        
        [modelArray enumerateObjectsUsingBlock:^(HomePageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            
            
            
            [muArray addObject:obj];
            
            
        }];
        
        self.modelArray = muArray.copy;
        
        
    } error:^{
        
        NSLog(@"error");
        
    }];
    
    pageIndex++;
}


@end





































